﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;

using System.Net;
using System.IO;
using DevDefined.OAuth.Consumer;
using DevDefined.OAuth.Framework;

namespace AbstractLabs.PowerShell.Yammer
{
	[Cmdlet(VerbsCommon.Set, "Yammer-Message")]
	public class TweetCmdlet : Cmdlet
	{
		[Parameter(Mandatory = true)]
		public string Message { get; set; }

		protected override void BeginProcessing()
		{
			base.BeginProcessing();
			bool expect100Continue = System.Net.ServicePointManager.Expect100Continue;
			try
			{
				System.Net.ServicePointManager.Expect100Continue = false;
				OAuthConsumerContext context = new OAuthConsumerContext();
				context.ConsumerKey = Properties.Settings.Default.ConsumerKey;
				context.ConsumerSecret = Properties.Settings.Default.ConsumerSecret;
				context.SignatureMethod = SignatureMethod.HmacSha1;
				OAuthSession session = new OAuthSession(context, (Uri)null, (Uri)null, (Uri)null);
				UriBuilder builder = new UriBuilder(Properties.Settings.Default.ApiUrl);
				builder.Path = String.Concat(builder.Path,"/messages.xml");
				string result = session.Request(Properties.Settings.Default.Token).Post().ForUri(builder.Uri).WithFormParameters(new { body = Message }).ToString();
				WriteVerbose(result);
			}
			catch (Exception exception)
			{
				WriteError(new ErrorRecord(exception, String.Empty, ErrorCategory.WriteError, null));
			}
			finally
			{
				System.Net.ServicePointManager.Expect100Continue = expect100Continue;
			}



		}
	}
}
